<?php

namespace app\proxy\controller;

use PHPQRCode\QRcode;
use think\Controller;
use think\Db;
use think\Session;

class Login extends Controller
{
    // 公共
    public function _initialize()
    {
        if (Session::has('proxy', 'ffxiangAd')) {
            $this->redirect("index/index");
        }
    }

    public function index()
    {
        if (request()->isAjax()) {
            $username = input('post.name');
            $password = input('post.password');
            $captcha = input('post.yzm');
            if ($username && $password) {
                if (!$captcha) {
                    return ['err' => '请输入验证码'];
                }
                if (!captcha_check($captcha)) {
                    return ['err' => '验证码错误'];
                };
                $time = time();
                $ip = _ip();

                $where = ['proxy' => 1];
                $where['phone'] = $username;
                // 读取用户账户是否存在
                $member = Db::name('user')->where($where)->find();
                if (!$member) {
                    exit(json_encode(['err' => '账户不存在']));
                }
                // 读取用户信息
                $where['password'] = $password;
                $member = Db::name('user')->where($where)->find();

                if (!$member) {
                    echo json_encode(['err' => '用户和密码不一致']);
                    exit();
                }

                $url = 'http://' . $_SERVER['HTTP_HOST'] . SITE_PATH . '/?yq='.$member['id'];

                $member['qrcode'] = $this->createTempQrcode($url);

                Session::set('proxy', $member, 'ffxiangAd');

                $data = array("ret" => '登录成功,正在跳转...');

            } else {
                $data = ['err' => '请输入信息'];
            }
            return json($data);
        } else {
            $icp = db('config_system')->where('title', 'bottom')->cache(86400)->value('content');

            $this->assign('icp', $icp);
            return $this->fetch();
        }
    }

    /**
     * 生成临时二维码图片
     * 这里返回的是base64进制图片
     * 一般用于微信扫码支付二维码生成场景
     *
     * @param string $data 二维码内容
     *         示例数据：http://www.tf4.cn或weixin://wxpay/bizpayurl?pr=0tELnh9
     *
     * @return
     */
    public function createTempQrcode($data)
    {
        $object = new QRcode();
        $errorCorrectionLevel = 'L';    //容错级别
        $matrixPointSize = 5;            //生成图片大小

        //打开缓冲区
        ob_start();
        //生成二维码图片
        $returnData = $object->png($data,false,$errorCorrectionLevel, $matrixPointSize, 2);
        //这里就是把生成的图片流从缓冲区保存到内存对象上，使用base64_encode变成编码字符串，通过json返回给页面。
        $imageString = base64_encode(ob_get_contents());
        //关闭缓冲区
        ob_end_clean();
        $base64 = "data:image/png;base64,".$imageString;

        $result['errcode'] = 0;
        $result['errmsg'] = 'ok';
        $result['data'] = $base64;

        return $base64;
    }
}
